﻿namespace KeRui.Upload
{
    using Microsoft.Win32;
    using System;
    using System.Collections;

    internal class MacFileType
    {
        private static uint BlankType = SuperStream.StringToOSType("----");
        private static ArrayList FileTypes = Init(new ArrayList());
        private const string RegPathMacFileExts = @"SYSTEM\CurrentControlSet\Services\MacFile\Parameters\Extensions\";
        private const string RegPathMacFilePars = @"SYSTEM\CurrentControlSet\Services\MacFile\Parameters\";
        private const string RegPathMacFileType = @"SYSTEM\CurrentControlSet\Services\MacFile\Parameters\Type_Creators\";

        private static void AddEntry(ArrayList inList, string inType, string inCreator, string inXt, string inDesc)
        {
            FileTypeEntry entry = new FileTypeEntry();
            entry.type = SuperStream.StringToOSType(inType);
            entry.creator = SuperStream.StringToOSType(inCreator);
            entry.xt = inXt;
            entry.desc = inDesc;
            inList.Add(entry);
        }

        public static string InferFileType(string inType, string inCreator)
        {
            return InferFileType(SuperStream.StringToOSType(inType), SuperStream.StringToOSType(inCreator));
        }

        public static string InferFileType(uint inType, uint inCreator)
        {
            string str = "";
            try
            {
                str = InferFileTypeSFM(inType, inCreator);
            }
            catch (Exception exception)
            {
                Log.UniqueError("Unable to read Services for Macintosh prefrences from Registry. " + exception.Message);
            }
            if (str == "")
            {
                str = InferFileTypeHW(inType, inCreator);
                if (str == "")
                {
                    str = SuperStream.OSTypeToString(inType);
                }
            }
            return str;
        }

        private static string InferFileTypeHW(uint inType, uint inCreator)
        {
            for (int i = 0; i < FileTypes.Count; i++)
            {
                FileTypeEntry entry = (FileTypeEntry) FileTypes[i];
                bool flag = (entry.type == inType) || (entry.type == BlankType);
                bool flag2 = (entry.creator == inCreator) || (entry.creator == BlankType);
                if (flag && flag2)
                {
                    return entry.xt;
                }
            }
            return "";
        }

        private static string InferFileTypeSFM(uint inType, uint inCreator)
        {
            string str = "TYPE=" + SuperStream.OSTypeToString(inType).Trim();
            string str2 = "CREATOR=" + SuperStream.OSTypeToString(inCreator).Trim();
            string str3 = null;
            string str4 = "";
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\MacFile\Parameters\Type_Creators\");
            string[] valueNames = key.GetValueNames();
            foreach (string str5 in valueNames)
            {
                try
                {
                    Array array = (Array) key.GetValue(str5);
                    if ((((string) array.GetValue(0)) == str2) && (((string) array.GetValue(1)) == str))
                    {
                        str3 = "ID=" + str5;
                        break;
                    }
                }
                catch
                {
                }
            }
            key.Close();
            if (str3 != null)
            {
                key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\MacFile\Parameters\Extensions\");
                valueNames = key.GetValueNames();
                foreach (string str6 in valueNames)
                {
                    try
                    {
                        Array array2 = (Array) key.GetValue(str6);
                        if (((string) array2.GetValue(0)) == str3)
                        {
                            str4 = str6;
                            break;
                        }
                    }
                    catch
                    {
                    }
                }
                key.Close();
            }
            return str4;
        }

        private static ArrayList Init(ArrayList inList)
        {
            AddEntry(inList, "AB65", "AD65", "P65", "Pagemaker 6.5");
            AddEntry(inList, "ALB4", "ALD4", "PM4", "Pagemaker 4");
            AddEntry(inList, "ALB5", "ALD5", "PM5", "Pagemaker 5");
            AddEntry(inList, "ALB6", "ALD6", "PM6", "Pagemaker 6");
            AddEntry(inList, "ART3", "TEXT", "AI", "Illustrator 3");
            AddEntry(inList, "ART4", "TEXT", "AI", "Illustrator");
            AddEntry(inList, "ART5", "TEXT", "AI", "Illustrator");
            AddEntry(inList, "DPRO", "dDOC", "MDP", "MacDraw Pro");
            AddEntry(inList, "FH50", "AGD1", "FH5", "Freehand 5");
            AddEntry(inList, "FHD3", "FHA3", "FH3", "Freehand 3");
            AddEntry(inList, "FMP3", "FMP3", "FP3", "FileMaker Pro");
            AddEntry(inList, "FXTC", "EggP", "AEP", "After Effects Project");
            AddEntry(inList, "InDd", "inDn", "IND", "Indesign Document");
            AddEntry(inList, "MV93", "MD93", "DIR", "Macromedia Director");
            AddEntry(inList, "StMI", "TEXT", "HTM", "PageMill HTML page");
            AddEntry(inList, "TEXT", "BNHQ", "HQX", "BinHex");
            AddEntry(inList, "WP42", "SSIW", "WP4", "Wordperfect 4");
            AddEntry(inList, "WP50", "SSIW", "WP5", "Wordperfect 5");
            AddEntry(inList, "ZIP ", "ZIP ", "ZIP", "Zip Archive");
            AddEntry(inList, "PICT", "----", "PCT", "Generic PICT");
            AddEntry(inList, "JPEG", "----", "JPG", "Generic JPEG");
            AddEntry(inList, "GIFf", "----", "GIF", "Generic GIF");
            AddEntry(inList, "PNGf", "----", "PNG", "Generic PNG");
            AddEntry(inList, "BMP ", "----", "BMP", "Generic BMP");
            AddEntry(inList, "TIFF", "----", "TIF", "Generic TIFF");
            AddEntry(inList, "EPSF", "----", "EPS", "Generic EPS");
            AddEntry(inList, "MooV", "----", "MOV", "Generic Movie");
            AddEntry(inList, "TEXT", "----", "TXT", "Generic Text");
            AddEntry(inList, "HTML", "----", "HTM", "Generic HTML");
            AddEntry(inList, "PDF ", "----", "PDF", "Portable Document Format");
            AddEntry(inList, "RTF ", "----", "RTF", "Rich Text Format");
            AddEntry(inList, "FMP3", "----", "FP3", "FileMaker Pro");
            AddEntry(inList, "XLBN", "----", "XLS", "Excel 1");
            AddEntry(inList, "XLS4", "----", "XLS", "Excel 4");
            AddEntry(inList, "XLS5", "----", "XLS", "Excel 5");
            AddEntry(inList, "XLS8", "----", "XLS", "Excel 98");
            AddEntry(inList, "sLS8", "----", "XLS", "Excel 98 Template");
            AddEntry(inList, "SLD3", "----", "PPT", "Power Point 3");
            AddEntry(inList, "SLD8", "----", "PPT", "Power Point 98");
            AddEntry(inList, "PPOT", "----", "PPT", "Power Point 98 Template");
            AddEntry(inList, "XDOC", "----", "QXD", "Quark Xpress (all versions)");
            AddEntry(inList, "W6BN", "----", "DOC", "Microsoft Word 6");
            AddEntry(inList, "W8BN", "----", "DOC", "Microsoft Word 98");
            AddEntry(inList, "WDBN", "----", "DOC", "Microsoft Word 3/4/5");
            AddEntry(inList, "WTBN", "----", "DOT", "Microsoft Word Template");
            AddEntry(inList, "8BIM", "----", "PSD", "Photoshop 3");
            AddEntry(inList, "8BPS", "----", "PSD", "Photoshop 3 (calques)");
            AddEntry(inList, "----", "BOBO", "CWK", "Claris Works");
            AddEntry(inList, "----", "FMP3", "FP3", "FileMaker Pro");
            AddEntry(inList, "----", "XCEL", "XLS", "Excel");
            AddEntry(inList, "----", "PPT3", "PPT", "Power Point");
            AddEntry(inList, "----", "XPR3", "QXD", "Quark Xpress");
            AddEntry(inList, "----", "MSWD", "DOC", "Microsoft Word");
            AddEntry(inList, "----", "8BIM", "PSD", "Photoshop Document");
            return inList;
        }

        public class FileTypeEntry
        {
            public uint creator;
            public string desc;
            public uint type;
            public string xt;
        }
    }
}

